//
//  YDOptionalNoDataView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/21.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDOptionalNoDataView.h"
#import "YDHomeReModel.h"

@interface YDOptionalNoDataView()
/////*****顶部控件********//////
@property (nonatomic , weak) UIView *topView;
@property (nonatomic , weak) UILabel *topPostLabel;
@property (nonatomic , weak) SYNoHeightedButton *searchButton;
/////*****底部控件********//////
@property (nonatomic , weak) UILabel *hotReLabel;
@property (nonatomic , weak) UIView *buttonsView;
@end

@implementation YDOptionalNoDataView

-(void)setTitleArray:(NSMutableArray *)titleArray
{
    _titleArray = titleArray;
    
    //创建按钮
    //离屏幕左右间距
    CGFloat marginLR = rate375(20);
    //上下间距
    CGFloat marginTD = isPhoneX ? rateX812(21) : rate750(21);
    //按钮之间的左右 间距
    CGFloat buttonMargin = rate375(30);
    
    //每行默认显示3个
    int colNumber = 3;
    CGFloat buttonW = (LKScreenW - (marginLR + buttonMargin) * 2) / 3;
    CGFloat buttonH = isPhoneX ? rateX812(34) : rate750(34);

    for (int i = 0; i < titleArray.count; i++) {
        NSInteger whichCow = i % colNumber;//哪一列
        NSInteger whichCol = i / colNumber;//哪一行
        CGFloat x = marginLR + whichCow * (buttonMargin + buttonW);
        
        CGFloat y = (marginTD + buttonH) * whichCol;

        SYNoHeightedButton *button = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.buttonsView addSubview:button];
        button.frame = CGRectMake(x, y, buttonW, buttonH);
        button.backgroundColor = [UIColor clearColor];
        button.tag = i;
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:blueyGrey] forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        button.backgroundColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    }

    
}

-(void)buttonClick:(SYNoHeightedButton *)button
{
    if ([self.delegate respondsToSelector:@selector(optionalNoDataView:buttonStyle:searchString:)]) {
        [self.delegate optionalNoDataView:self buttonStyle:SYOptionalNoDataViewStyleSearch searchString:button.titleLabel.text];
    }
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = NumberColor(247, 247, 247);
        self.titleArray = [self getDefaultStocks];
        [self topView];
        [self topPostLabel];
        [self searchButton];
        
        [self hotReLabel];
        
        [self buttonsView];
    }
    return self;
}

+(instancetype)optionalNoDataViewWithTitleArray:(NSArray *)titleArray
{
    YDOptionalNoDataView *dataView = [[self alloc] init];

    return dataView;
}
//从首页缓存的数据中获取6个推荐股票
-(NSMutableArray *)getDefaultStocks
{
    NSArray *array = @[@"平安银行" , @"万科 A" , @"国农科技" , @"世纪星源" , @"神州高铁" , @"中国宝安"];
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    if ([store isExistTableWithName:storeHomeDataKey]) {
        //获取交易动态[store getObjectById:dataId fromTable:storeHomeDataKey];
        id reDataObj = [store getObjectById:storeHomeReDataID fromTable:storeHomeDataKey];
        if (reDataObj) {
            NSArray *reDataArray = (NSArray *)reDataObj;
            NSArray *modelArray = [YDHomeReModel mj_objectArrayWithKeyValuesArray:reDataArray];
            NSMutableArray *stockTitleArray = [NSMutableArray array];
            for (YDHomeReModel *model in modelArray) {
                [stockTitleArray addObject:model.name];
            }
            
            if (stockTitleArray.count) {
                if (stockTitleArray.count > 6) {//只显示6个
                    return [[stockTitleArray subarrayWithRange:NSMakeRange(0, 6)] mutableCopy];
                }else{
                   return stockTitleArray;
                }
            }else{
                return [array mutableCopy];
            }
            
        }else{

            return [array mutableCopy];
        }
    }else{//不存在
        return [array mutableCopy];
    }
}
/////*****顶部控件********//////
-(UIView *)topView
{
    if (_topView == nil) {
        UIView *topView = [[UIView alloc] init];
        [self addSubview:topView];
        topView.backgroundColor = [UIColor whiteColor];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(160)));
            }else{
                make.height.equalTo(@(rate750(160)));
            }
        }];
        _topView = topView;
    }
    return _topView;
}


-(UILabel *)topPostLabel
{
    if (_topPostLabel == nil) {
        UILabel *topPostLabel = [[UILabel alloc] init];
        [self.topView addSubview:topPostLabel];
        topPostLabel.textColor = [UIColor colorWithHexString:blueyGrey];
        topPostLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        topPostLabel.textAlignment = NSTextAlignmentCenter;
        topPostLabel.text = @"您的自选股目前为空，赶紧去添加吧！";
        [topPostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.topView);
            make.width.equalTo(@(rate375(301)));
            if (isPhoneX) {
                make.top.equalTo(self.topView).offset(rateX812(46));
            }else{
                make.top.equalTo(self.topView).offset(rate750(46));
            }
        }];
        topPostLabel.adjustsFontSizeToFitWidth = YES;
        _topPostLabel = topPostLabel;
    }
    return _topPostLabel;
}
-(SYNoHeightedButton *)searchButton
{
    if (_searchButton == nil) {
        SYNoHeightedButton *searchButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.topView addSubview:searchButton];
        searchButton.backgroundColor = [UIColor clearColor];
        [searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.topView);
            make.width.equalTo(@(rate375(143)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(36)));
                make.top.equalTo(self.topView).offset(rateX812(87));
            }else{
                make.height.equalTo(@(rate750(36)));
                make.top.equalTo(self.topView).offset(rate750(87));
            }
        }];
        [searchButton setTitleColor:[UIColor colorWithHexString:battleshipGrey] forState:UIControlStateNormal];
        [searchButton setTitle:@"搜索股票" forState:UIControlStateNormal];
        searchButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        [searchButton addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchDown];
        searchButton.layer.cornerRadius = 4.0f;
        searchButton.layer.borderColor = [UIColor colorWithHexString:battleshipGrey].CGColor;
        searchButton.layer.borderWidth = 1.0f;
        searchButton.layer.masksToBounds = YES;
        
        
        _searchButton = searchButton;
    }
    return _searchButton;
}
-(void)searchButtonClick:(SYNoHeightedButton *)button
{
    if ([self.delegate respondsToSelector:@selector(optionalNoDataView:buttonStyle:searchString:)]) {
        [self.delegate optionalNoDataView:self buttonStyle:SYOptionalNoDataViewStyleDefault searchString:@""];
    }
}


-(UILabel *)hotReLabel
{
    if (_hotReLabel == nil) {
        UILabel *hotReLabel = [[UILabel alloc] init];
        [self addSubview:hotReLabel];
        hotReLabel.textColor = [UIColor colorWithHexString:blueyGrey];
        hotReLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        hotReLabel.textAlignment = NSTextAlignmentCenter;
        hotReLabel.text = @"热门股票推荐";
        [hotReLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(rate375(20));
            if (isPhoneX) {
                make.top.equalTo(self.topView.mas_bottom).offset(rateX812(30));
            }else{
                make.top.equalTo(self.topView.mas_bottom).offset(rate750(30));
            }
        }];
        _hotReLabel = hotReLabel;
    }
    return _hotReLabel;
}

-(UIView *)buttonsView
{
    if (_buttonsView == nil) {
        UIView *buttonsView = [[UIView alloc] init];
        [self addSubview:buttonsView];
        buttonsView.backgroundColor = [UIColor clearColor];
        [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(105)));
                make.top.equalTo(self.topView.mas_bottom).offset(rateX812(70));
            }else{
                make.height.equalTo(@(rate750(105)));
                make.top.equalTo(self.topView.mas_bottom).offset(rate750(70));
            }
        }];
        _buttonsView = buttonsView;
    }
    return _buttonsView;
}


@end
